<template>
  <Modal
    title="编辑"
    :value="value"
    :mask-closable="true"
    :footer-hide="true"
    @input="$emit('input', $event)"
  >
    <Form
      ref="formLkyApply"
      :model="formLkyApply"
      :rules="ruleValidate"
      label-position="right"
      :label-width="80"
    >

      <FormItem label="表名" prop="name">
        <Input type="text" v-model="formLkyApply.file_name" placeholder="表名"/>
      </FormItem>
      <FormItem label="申请理由" prop="class_number">
        <Input type="text"  placeholder="申请理由"/>
      </FormItem>
      <FormItem>
        <Button type="primary" @click="handleSubmit('formLkyClass')">提交</Button>
        <Button @click="handleReset('formLkyClass')" style="margin-left: 8px">重置</Button>
      </FormItem>

    </Form>
  </Modal>
</template>
<script>
import { update } from '@/api/lky/lkyClass'
import { createModelObj, coverProps, coverFormProps } from '@/libs/util'
// 学院查询、专业查询
import { queryAll } from '@/api/lky/lkyCollege'
import { queryAllM } from '@/api/lky/lkyMajor'

export default {
  name: 'Edit',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    lkyApply: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      collegeList: [],
      coid: '',
      // 这个是修改的
      mid: '',
      majorList: [],
      formLkyApply: {
        id: '',
        fife_name: '',
        times: '',
        count: '',
        start_time: '',
        end_time: '',
        mid: ''
      },
      ruleValidate: {

        file_name: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 20, message: '长度最长为20', trigger: 'blur' }
        ],
        // class_number: [
        //   { required: true, message: '该值不能为空', trigger: 'blur' },
        //   { type: 'string', max: 20, message: '长度最长为20', trigger: 'blur' }
        // ],
        mid: [
          { required: false, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 32, message: '长度最长为32', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onOpenChangeCollege () {
      queryAll().then(res => {
        // console.log(res)
        this.collegeList = res.data
        this.formLkyApply.mid = ''
      })
    },
    onOpenChangeMajor () {
      queryAllM({ 'coid': this.coid }).then(res => {
        this.majorList = res.data
      })
    },

    handleSubmit (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          var obj = {}
          obj.id = this.formLkyApply.id
          obj.file_name = this.fromLkyApply.file_name
          obj.times = this.fromLkyApply.times
          obj.count = this.fromLkyApply.count
		      obj.start_time = this.fromLkyApply.start_time
		      obj.end_time = this.fromLkyApply.end_time 	
		      obj.mid = this.fromLkyApply.mid
          update(
            Object.assign({}, createModelObj(obj, 'lkyApply'))
          ).then(res => {
            if (res) {
              this.$emit('updateRow', coverProps(this.lkyApply, this.formLkyApply))
              this.$emit('input', false)
              this.handleReset(name)
            }
          })
        } else {
          this.$Message.error('验证失败!')
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields()
    }
  },
  watch: {
    value: function (val, oldVal) {
      this.$refs.formLkyApply.resetFields()
      coverFormProps(this.formLkyApply, this.lkyApply)
    },
    // 获取学院id
    lkyApply (n) {
      if (n != null) {
        this.coid = n.id2
        this.formLkyApply.mid = n.mid
        this.onOpenChangeMajor()
      }
    }
  },
  // 这里
  mounted () {
    this.onOpenChangeCollege()
  }
}
</script>
